﻿Public Class frmGenerateModel

    Dim model As New SystemDatabase()
    Dim dtGridview As DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call setCmbDatabase()

    End Sub

    Private Sub btnSelectDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectDatabase.Click
        Dim dbName As String = cmbDatabase.SelectedValue.ToString()

        If RTables.Checked Then
            Call setCmbTable(dbName, "tables")
        Else
            Call setCmbTable(dbName, "views")
        End If





    End Sub

    Private Sub btnSelectTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectTable.Click

        Call setGridView()

    End Sub

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        Dim tableName As String = cmbTable.SelectedValue.ToString()

        If DataGridView1.RowCount > 0 Then
            Dim lang As String = String.Empty

            If rbCSharp.Checked = True Then
                lang = "C#"
            ElseIf rbVB.Checked = True Then
                lang = "VB"
            End If

            If cbModel.Checked = True Then

                Dim gen As New ModelClass()
                '*** Set ClassName ***'
                gen.className = tableName
                gen.dtGridview = dtGridview
                gen.Generate(lang)
                MsgBox("Generate Class Model Success")
            End If

            If cbProcedure.Checked = True Then
                MsgBox("Generate Store Procedure" & lang)
            End If
        Else
            MsgBox("Please Select Database Or Table !!!")
        End If

    End Sub

#Region "*** Getter Setter Method ***"

    Private Sub setCmbDatabase()

        Dim model As New SystemDatabase()
        Dim dt As DataTable = model.getListDBName()
        cmbDatabase.DataSource = dt.DefaultView
        cmbDatabase.DisplayMember = "Name"
        cmbDatabase.ValueMember = "Name"

    End Sub

    Private Sub setCmbTable(ByVal dbName As String, ByVal schema As String)

        Dim dt As DataTable = model.getListTable(dbName, schema)
        cmbTable.DataSource = dt.DefaultView
        cmbTable.DisplayMember = "TABLE_NAME"
        cmbTable.ValueMember = "TABLE_NAME"

    End Sub

    Private Sub setGridView()
        Dim tableName As String = cmbTable.SelectedValue.ToString()

        dtGridview = model.getFieldTable(tableName)

        DataGridView1.DataSource = dtGridview

    End Sub

#End Region





    Private Sub cmbTable_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTable.SelectedIndexChanged
        Call setGridView()
    End Sub
End Class
